package com.chase.passwordmaster.entity

import com.chase.passwordmaster.encoder.decoded
import com.chase.passwordmaster.encoder.encoded

// 不敢继承Account 以后稳定了再测试
// 加密应该是在这一层的，而不是Account
open class DecryptedAccount(private val account: Account) {
    var website: String
        get() = account.website
        set(value) {
            account.website = value
        }

    var username: String
        get() = account.username.decoded()
        set(value) {
            account.username = value.encoded()
        }

    var password: String
        get() = account.password.decoded()
        set(value) {
            account.password = value.encoded()
        }

    var name: String?
        get() = account.name
        set(value) {
            account.name = value
        }

    var tel: String?
        get() = account.tel?.decoded()
        set(value) {
            account.tel = value?.encoded()
        }

    var qq: String?
        get() = account.qq?.decoded()
        set(value) {
            account.qq = value?.encoded()
        }

    var weChat: String?
        get() = account.weChat?.decoded()
        set(value) {
            account.weChat = value?.encoded()
        }

    var email: String?
        get() = account.email?.decoded()
        set(value) {
            account.email = value?.encoded()
        }

    var memo: String?
        get() = account.memo
        set(value) {
            account.memo = value
        }

    var id: Long?
        get() = account.id
        set(value) {
            account.id = value
        }

    constructor(
        website: String,
        username: String,
        password: String,
        name: String? = null,
        tel: String? = null,
        qq: String? = null,
        weChat: String? = null,
        email: String? = null,
        memo: String? = null,
        id: Long? = null
    ) : this(
        account = Account(
            website = website,
            username = username.encoded(),
            password = password.encoded(),
            name = name,
            tel = tel?.encoded(),
            qq = qq?.encoded(),
            weChat = weChat?.encoded(),
            email = email?.encoded(),
            memo = memo,
            id = id
        )
    )

    fun save() = account.save()

    fun delete() = account.delete()

    fun setToDefault(fieldName: String) = account.setToDefault(fieldName)

    fun update(id: Long) = account.update(id)

    fun updateAll(vararg conditions: String) = account.updateAll(*conditions)
}